package no.uio.inf5750.wsclient.javarosaclient;

import java.util.Hashtable;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

import org.javarosa.core.Context;
import org.javarosa.core.api.Constants;
import org.javarosa.core.api.IActivity;
import org.javarosa.core.api.IShell;

import de.enough.polish.ui.Container;
import de.enough.polish.ui.Form;
import de.enough.polish.ui.ImageItem;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemCommandListener;
import de.enough.polish.ui.StringItem;

public class WelcomeActivity
    implements IActivity, CommandListener, ItemCommandListener
{

    public static final String CMD_KEY = "command";
    public static final String CMD_EXIT = "exit";
    public static final String CMD_DATASET_ADDUPDATE = "add/update dataset";
    public static final String CMD_DATASET_SIMPLIFIED_ADDUPDATE = "simplified add/update dataset";

    private IShell shell;

    private Form form;

    private Command cmdExit;
    private Command cmdOk;

    private Container menu;
    private StringItem miAddUpdateDataset;
    private StringItem miSimplifiedAddUpdateDatasetSpike;

    public WelcomeActivity( IShell shell )
    {
        setShell( shell );
        createWelcomeForm();
    }

    public void contextChanged( Context globalContext )
    {
        // TODO Auto-generated method stub

    }

    public void destroy()
    {
        // TODO Auto-generated method stub

    }

    public Context getActivityContext()
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void halt()
    {
        // TODO Auto-generated method stub

    }

    public void resume( Context globalContext )
    {
        shell.setDisplay( this, Util.createIView( form ) );
    }

    public void setShell( IShell shell )
    {
        this.shell = shell;
    }

    public void start( Context context )
    {
        shell.setDisplay( this, Util.createIView( form ) );
    }

    private void createWelcomeForm()
    {
        //#style .welcomeScreen
        form = new Form( null );

        // Only show logo if Polish is active TODO: verify that logo is not shown if usePolishGui==false
        //#ifdef polish.usePolishGui 
        //#style .welcomeLogo
        ImageItem logo = new ImageItem( null, Util.createImage( "/logo_banner.png" ), Item.LAYOUT_CENTER, "DHIS 2 Logo" );
        form.append( logo );
        //#endif

        //#style .welcomeTitle
        form.append( "Welcome to the DHIS mobile client. Please select an activity from the list below." );

        // Add form-commands
        cmdExit = new Command( "Exit", Command.EXIT, 1 );
        form.addCommand( cmdExit );

        // Add menu items with commands
        //#style welcomeMenu
        menu = new Container( true );
        cmdOk = new Command( "Start activity", Command.OK, 0 );

        miAddUpdateDataset = createMenuItem( "Add/Update dataset", cmdOk );
        miSimplifiedAddUpdateDatasetSpike = createMenuItem( "Simplified add/update dataset (Spike)", cmdOk );

        menu.add( miAddUpdateDataset );
        menu.add( miSimplifiedAddUpdateDatasetSpike );

        // Append menu container to form
        form.append( menu );

        form.setCommandListener( this );
    }

    private StringItem createMenuItem( String text, Command cmd )
    {
        //#style .welcomeMenuItem
        StringItem item = new StringItem( null, text );
        item.setDefaultCommand( cmd );
        item.setItemCommandListener( this );
        return item;
    }

    public void commandAction( Command cmd, Displayable disp )
    {
        System.out.println( "Cmd: " + cmd.getLabel() );

        if ( cmd == cmdExit )
        {
            Hashtable returnArgs = new Hashtable();
            returnArgs.put( CMD_KEY, CMD_EXIT );
            shell.returnFromActivity( this, Constants.ACTIVITY_COMPLETE, returnArgs );
        }
    }

    public void commandAction( Command cmd, Item item )
    {
        System.out.println( "Cmd: " + cmd.getLabel() + ", Item: " + item.toString() );

        if ( cmd == cmdOk )
        {
            Hashtable returnArgs = new Hashtable();

            if ( item == miAddUpdateDataset )
            {
                returnArgs.put( CMD_KEY, CMD_DATASET_ADDUPDATE );
                shell.returnFromActivity( this, Constants.ACTIVITY_NEEDS_RESOLUTION, returnArgs );
            }
            else if ( item == miSimplifiedAddUpdateDatasetSpike )
            {
                returnArgs.put( CMD_KEY, CMD_DATASET_SIMPLIFIED_ADDUPDATE );
                shell.returnFromActivity( this, Constants.ACTIVITY_NEEDS_RESOLUTION, returnArgs );
            }
        }
    }

}
